package edu.gatech.stolen.tea.login;

import edu.gatech.stolen.tea.Accounts;
import edu.gatech.stolen.tea.R;
import edu.gatech.stolen.tea.R.id;
import edu.gatech.stolen.tea.R.layout;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class RegisterActivity extends Activity {
	
	public static final String TAG = "Register Page";
	
	private EditText fName;
	private EditText lName;
	private EditText uName;
	private EditText pWord;
	private EditText eMail;
	private Button regButton;
	private Button canButton;
	private TextView registerInfo;
	private String username, password;
	private Accounts accounts= new Accounts();
	
	
    /** Called when the activity is first created.
     * initialize the buttons and text fields
     */
    @Override
    public void onCreate(Bundle savedInstanceState) {
    	
    	Log.v(TAG, "Activity State: onCreate()");
        super.onCreate(savedInstanceState);
        setContentView(R.layout.register);
        if ( accounts.loadData(getFilesDir().toString()) != null){
			accounts=accounts.loadData(getFilesDir().toString());
		}
		else{
			accounts.saveData(getFilesDir().toString());
		}
        
        fName = (EditText) findViewById(R.id.eFirstEditText_register);
        lName = (EditText) findViewById(R.id.eLastEditText_register);
        uName = (EditText) findViewById(R.id.eUsernameEditText_register);
        pWord = (EditText) findViewById(R.id.ePasswordEditText_register);
        eMail = (EditText) findViewById(R.id.eEMailEditText_register);
        regButton = (Button) findViewById(R.id.bRegisterButton_register);
        canButton = (Button) findViewById(R.id.bCancelButton_register);
        registerInfo = (TextView) findViewById(R.id.lblRegisterInfo);
        
        /**
         * give register button a click listener
         */
        regButton.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				
				onRegButtonClicked();
				
			}
        });
        
        /**
         * give cancel button a register listener
         */
        canButton.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				
				onCanButtonClicked();
				
			}
        });
        
    	
    }
    /**
     * if register fails, notify the user
     * if register successful, go to registered page
     */
    private void onRegButtonClicked() {
    	Log.v(TAG, "Register Button Clicked");
    	username = uName.getText().toString();
    	password = pWord.getText().toString();
    	if(username.equals("") || password.equals("")){
    		registerInfo.setText("Registration failed. Please fill in all blanks.");
    	} else {
	    	boolean flag = accounts.createUser(username,password);
	    	if(flag){
	    		accounts.saveData(getFilesDir().toString());
	        	goToRegisteredPage();
	    	} else {
	    		registerInfo.setText("Registration failed. Username already registered.");
	    	} 	
    	}
    }
    
    /**
     * if cancel button clicked, go back to register page
     */
    private void onCanButtonClicked() {
    	
    	Log.v(TAG, "Register Button Clicked");
    	goToLoginPage();
    	
    }
    
    /**
     * go to register successful page
     */
    protected void goToRegisteredPage() {
    	
    	Intent i = new Intent(this, Registered.class);
    	startActivity(i);
   	
    }
    
    /**
     * go to login page
     */
    protected void goToLoginPage() {
    	
    	Intent i = new Intent(this, LoginActivity.class);
    	startActivity(i);
    	
    }  

}